package repository

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/vo"
	"boy-go/pkg/db"
	"boy-go/pkg/request"
	"gorm.io/gorm"
	"time"
)

var (
	_selectSysNoticeVo = "notice_id, notice_title, notice_type, notice_content, status, create_by, create_time, update_by, update_time, remark"
)

type SysNoticeRepository struct {
	DB *gorm.DB
}

func NewSysNoticeRepository() *SysNoticeRepository {
	return &SysNoticeRepository{
		DB: db.GetMasterDB(),
	}
}
func (r *SysNoticeRepository) GetById(noticeId int64) (*model.SysNotice, error) {
	db := r.DB
	m := &model.SysNotice{}
	err := db.Model(&model.SysNotice{}).Where("notice_id = ?", noticeId).First(m).Error
	if err != nil {
		return nil, err
	}
	return m, nil
}

func (r *SysNoticeRepository) List(query *vo.QuerySysNoticeReq, pageReq *request.PageRequest) ([]*model.SysNotice, int64, error) {
	tx := r.DB.Model(&model.SysNotice{})
	if query != nil {
		if query.NoticeTitle != nil {
			tx = tx.Where("notice_title = ?", *query.NoticeTitle)
		}
		if query.NoticeType != nil {
			tx = tx.Where("notice_type = ?", *query.NoticeType)
		}
		if query.NoticeContent != nil {
			tx = tx.Where("notice_content = ?", *query.NoticeContent)
		}
		if query.Status != nil {
			tx = tx.Where("status = ?", *query.Status)
		}
		if query.CreateBy != nil {
			tx = tx.Where("create_by = ?", *query.CreateBy)
		}
		if query.CreateTime != nil {
			tx = tx.Where("create_time = ?", *query.CreateTime)
		}
		if query.UpdateBy != nil {
			tx = tx.Where("update_by = ?", *query.UpdateBy)
		}
		if query.UpdateTime != nil {
			tx = tx.Where("update_time = ?", *query.UpdateTime)
		}
		if query.Remark != nil {
			tx = tx.Where("remark = ?", *query.Remark)
		}
	}
	list := make([]*model.SysNotice, 0)
	if pageReq != nil {
		var total int64
		if err := tx.Count(&total).Error; err != nil {
			return nil, 0, err
		}
		if total > 0 {
			tx = tx.Select(_selectSysNoticeVo)
			tx = db.BuildPageAndOrder(tx, &query.PageRequest, "")
			if err := tx.Find(&list).Error; err != nil {
				return nil, total, err
			}
		}
		return list, total, nil
	} else {
		tx = tx.Select(_selectSysNoticeVo)
		if err := tx.Find(&list).Error; err != nil {
			return nil, 0, err
		}
	}
	return list, int64(len(list)), nil
}

// Save 添加
func (r *SysNoticeRepository) Save(m *model.SysNotice) error {
	currentTime := time.Now()
	m.CreateAt = &currentTime
	return r.DB.Model(&model.SysNotice{}).Create(m).Error
}

// Update 更新
func (r *SysNoticeRepository) Update(m *model.SysNotice) error {
	currentTime := time.Now()
	m.UpdateAt = &currentTime
	return r.DB.Model(&model.SysNotice{}).Where("notice_id = ?", m.NoticeId).Updates(m).Error
}

// Delete 删除
func (r *SysNoticeRepository) Delete(noticeId int64) error {
	return r.DB.Model(&model.SysNotice{}).Where("notice_id = ?", noticeId).Delete(&model.SysNotice{}).Error
}

// DeleteBatch 批量删除
func (r *SysNoticeRepository) BatchDelete(ids []string) error {
	db := r.DB
	db = db.Where("(notice_id) IN ?", ids)
	return db.Delete(&model.SysNotice{}).Error
}
